/****************************************************
	文件：CommandPattern.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/5/5 20:42:54
	功能：命令模式
*****************************************************/

using System.Collections.Generic;
using UnityEngine;

namespace DesignPatterns
{
    public class CommandPattern : MonoBehaviour
    {
        private void Start()
        {
            //命令模式：
            //将命令请求者 与 命令执行者分离
            //容易实现命令队列（便于记录、撤销、重做命令）
            Invoker invoker = new Invoker();
            ReceiverA receiveA = new ReceiverA();//命令执行者
            CommandA commandA = new CommandA(receiveA);
            CommandB commandB = new CommandB(receiveA);
            //添加命令
            invoker.AddCommand(commandA);
            invoker.AddCommand(commandB);
            //执行命令
            invoker.ExecuteCommand();
        }

        /// <summary>
        /// 命令基类
        /// </summary>
        public abstract class CommandBase
        {
            protected ReceiverBase mReceiver;
            public CommandBase(ReceiverBase receiver)
            {
                mReceiver = receiver;
            }
            public abstract void Execute();
        }
        public class CommandA : CommandBase
        {
            public CommandA(ReceiverBase receiver) : base(receiver)
            {
            }
            public override void Execute()
            {
                Debug.Log("命令A");
                mReceiver.Execute();
            }
        }
        public class CommandB : CommandBase
        {
            public CommandB(ReceiverBase receiver) : base(receiver)
            {
            }

            public override void Execute()
            {
                Debug.Log("命令B");
                mReceiver.Execute();
            }
        }
        /// <summary>
        /// 命令接收者基类（执行命令的对象）
        /// </summary>
        public abstract class ReceiverBase
        {
            public abstract void Execute();
        }
        /// <summary>
        /// 接收命令者A
        /// </summary>
        public class ReceiverA : ReceiverBase
        {
            public override void Execute()
            {
                Debug.Log("ReceiverA 执行 命令");
            }
        }
        public class Invoker
        {
            /// <summary>
            /// 命令列表
            /// </summary>
            private List<CommandBase> mCommandList;
            public Invoker()
            {
                mCommandList = new List<CommandBase>();
            }
            /// <summary>
            /// 添加命令
            /// </summary>
            public void AddCommand(CommandBase command)
            {
                mCommandList.Add(command);
            }
            /// <summary>
            /// 移除命令
            /// </summary>
            public void RemoveCommand(CommandBase command)
            {
                mCommandList.Remove(command);
            }
            /// <summary>
            /// 执行命令列表中的命令
            /// </summary>
            public void ExecuteCommand()
            {
                foreach (CommandBase command in mCommandList)
                {
                    command.Execute();
                }
            }
        }
    }
}
